package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.dto.Club;
import utility.Database;
import utility.Log;

public class ClubModel extends Database {
	public boolean insert(Club dto) {
		String sql = "insert into tbl_club(club_id,club_name,club_image,city,stadium,league_id,manager_id) values(null,?,?,?,?,?,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getImage());
			pstmt.setString(3, dto.getCity());
			pstmt.setString(4, dto.getStadium());
			pstmt.setInt(5, dto.getLeagueId());
			pstmt.setInt(6, dto.getManagerId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int club_id) {
		String sql = "delete from tbl_club where club_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, club_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Club dto) {
		String sql = "update tbl_club set club_name=?,club_image=?,city=?,stadium=?,league_id=?,manager_id=? where club_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getImage());
			pstmt.setString(3, dto.getCity());
			pstmt.setString(4, dto.getStadium());
			pstmt.setInt(5, dto.getLeagueId());
			pstmt.setInt(6, dto.getManagerId());
			pstmt.setInt(7, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Club get(int club_id) {
		String sql = "select * from tbl_club where club_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, club_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Club dto = new Club();
				dto.setId(club_id);
				dto.setName(rs.getString("club_name"));
				dto.setImage(rs.getString("club_image"));
				dto.setCity(rs.getString("city"));
				dto.setStadium(rs.getString("stadium"));
				dto.setLeagueId(rs.getInt("league_id"));
				dto.setManagerId(rs.getInt("manager_id"));
				return dto;
			}
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_club where club_name like ? ORDER BY club_id DESC limit ?,?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(int limit, int page) {
		return list("", limit, page);
	}

	public int count(String search) {
		String sql = "select count(*) from tbl_club where club_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return 0;
	}

	public int count() {
		return count("");
	}
}
